<template>
  <a-drawer
    title="权限编辑"
    :destroyOnClose="true"
    width="360px"
    placement="right"
    @close="visible=false"
    :visible="visible"
  >
    <a-spin :spinning="loading">
      <a-tree v-if="allRole.length>0" :checkable="true"
              :treeData="allRole"
              :multiple="true"
              :defaultExpandAll="true"
              :checkedKeys="checkRole"
              @check="check"
              :selectable="false">

      </a-tree>


      <a-button style="margin: 10px 10px" type="primary" @click="commit">保存权限</a-button>
    </a-spin>
  </a-drawer>
</template>

<script>
  import { saveRoleAuth, selectAuthority } from '../../../../api/base/role'

  export default {
    name: 'authority',
    data() {
      return {
        visible: false,
        allRole: [],
        checkRole: [],
        halfCheck: [],
        loading: false,
        roleId: null,
      }
    },
    methods: {
      edit(data) {
        this.roleId=data.id
        this.visible = true
        this.allRole = []
        this.checkRole = []
        this.halfCheck = []
        this.loading = true
        selectAuthority(data).then(res => {
          if (res.status === 0) {
            this.allRole = this.deepAllRole(res.data.all)
            let checkIds = []
            this.deepRole(res.data.all, res.data.role, checkIds)
            this.checkRole = checkIds
          } else {
            this.$message.error(res.msg)
          }
          this.loading = false
        })
      },
      deepAllRole(role) {
        if (role) {
          return role.map(item => {
            item.key = item.id
            item.title = item.menuName
            if (item.children) {
              item.children = this.deepAllRole(item.children)
            }
            return item
          })
        } else {
          return []
        }
      },
      deepRole(roles, checks, checkIds) {
        let rs = true
        roles.forEach(item => {
          if (item.children && item.children.length > 0) {
            if (!this.deepRole(item.children, checks, checkIds)) {
              rs = false
            } else {
              let checkId = this.findRoleId(item.id, checks)
              if (checkId) {
                checkIds.push(checkId)
              } else {
                rs = false
              }
            }
          } else {
            let checkId = this.findRoleId(item.id, checks)
            if (checkId) {
              checkIds.push(checkId)
            } else {
              rs = false
            }
          }
        })
        return rs
      },
      findRoleId(id, checks) {
        let role = checks.find(item => item.id === id)
        return role ? role.id : null
      },
      commit() {

        this.loading = true
        saveRoleAuth({ id: this.roleId, ids: JSON.stringify([...this.checkRole, ...this.halfCheck]) }).then((res) => {
          if (res.status === 0) {
            this.$message.success('权限更新成功')
          } else {
            this.$message.error(res.msg)
          }
          this.loading = false
        })
      },
      check(checkedKeys, e) {
        this.checkRole = checkedKeys
        this.halfCheck = e.halfCheckedKeys
      }
    }
  }
</script>

<style scoped>

</style>
